package com.zhan.ielts.web.controller.article;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.Result;
import com.zhan.ielts.domain.article.Article;
import com.zhan.ielts.domain.article.query.ArticleQuery;
import com.zhan.ielts.domain.article.query.CategoryQuery;
import com.zhan.ielts.domain.article.vo.ArticleVo;
import com.zhan.ielts.domain.article.vo.CategoryChildVo;
import com.zhan.ielts.service.article.ArticleService;
import com.zhan.ielts.service.article.CategroyService;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseControllerImpl<Article, ArticleQuery> {
    private Logger log = LoggerFactory.getLogger(ArticleController.class);
    @Autowired
    ArticleService articleService;

    @Autowired
    CategroyService categroyService;

    @Override
    protected BaseService<Article> getBaseService() {
        return articleService;
    }

    @ResponseBody
    @RequestMapping(value = "/fetch/data", method = RequestMethod.GET)
    public Result getCategories() {
        try {
            long now = System.currentTimeMillis();
            log.info("start time ===>" +now);
            List<String> catidList = categroyService.getCategoryDataFromServer();
            articleService.getArticleFromServer(catidList);
            articleService.insertAllHotArticle();
//            List<String> catidList = categroyService.getCategoryDataFromServer();
//            articleService.updateArticle(catidList);
//            articleService.insertHotArticle();
            log.info("duration time ===>" +(System.currentTimeMillis() -  now));
        } catch (Exception error) {
            error.printStackTrace();
            return new Result(Result.Status.ERROR, "网络或系统错误");
        }
        return new Result(Result.Status.OK, "拉取成功");
    }


    @Override
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView selectList(ArticleQuery query, @PageableDefault(sort = {"publish_time"}, direction = Sort.Direction.DESC ) Pageable pageable) {
        ModelAndView mav = null;
        try {
            if (!TextUtils.isEmpty(query.getCategoryName())){
                CategoryQuery category = new CategoryQuery();
                category.setName(query.getCategoryName());
                List<String> categoryIdList = new ArrayList<>();
                List<CategoryChildVo> categoryList = categroyService.getChildCategories(category);
                if (null != categoryList && categoryList.size() > 0) {
                    for (int i = 0; i < categoryList.size(); i++) {
                        categoryIdList.add(categoryList.get(i).getCategoryId());
                    }
                    query.setCategoryIds(categoryIdList);
                }
            }

            Page<ArticleVo> page = articleService.queryArticleVoPageList(query, pageable);
            mav = new ModelAndView("/article/listArticle", "page", page);
            mav.addObject("query", query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }


    @ResponseBody
    @RequestMapping(value = "/pushmessage/{id}", method = RequestMethod.POST)
    public Result pushMessage(@PathVariable("id") String id) {
        Map map = new HashMap();
        try {
            Article article = articleService.queryById(id);
            if (article != null) {
                String mess = articleService.pushMessage(article);
                if ("fail".equals(mess)) {
                    return new Result(Result.Status.ERROR, "网络或系统错误！");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
             return new Result(Result.Status.ERROR, "网络或系统错误！");
        }

         return new Result(Result.Status.OK, map);
    }
}
